﻿using Api.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Api.DAL
{
    /// <summary>
	/// 数据访问类:case_info_notice
	/// </summary>
	public partial class case_info_notice
    {
        public case_info_notice()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int instance_id, int case_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from eazy_case_info_notice");
            strSql.Append(" where instance_id=@instance_id and case_id=@case_id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4),
                    new SqlParameter("@case_id", SqlDbType.Int,4)           };
            parameters[0].Value = instance_id;
            parameters[1].Value = case_id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists_I(int instance_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from eazy_case_info_notice");
            strSql.Append(" where instance_id=@instance_id");
            SqlParameter[] parameters = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4)};
            parameters[0].Value = instance_id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Models.case_info_notice model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into eazy_case_info_notice(");
            strSql.Append("instance_id,case_id,notice_code,notified_name,company_name,causes,zhifarenyuan,add_time,huizhiurl,number )");
            strSql.Append(" values (");
            strSql.Append("@instance_id,@case_id,@notice_code,@notified_name,@company_name,@causes,@zhifarenyuan,@add_time,@huizhiurl,@number )");
            SqlParameter[] parameters = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4),
                    new SqlParameter("@case_id", SqlDbType.Int,4),
                    new SqlParameter("@notice_code", SqlDbType.VarChar,50),
                    new SqlParameter("@notified_name", SqlDbType.NVarChar,150),
                    new SqlParameter("@company_name", SqlDbType.NVarChar,250),
                    new SqlParameter("@causes", SqlDbType.NVarChar,550),
                    new SqlParameter("@zhifarenyuan", SqlDbType.NVarChar,750),
                    new SqlParameter("@add_time", SqlDbType.DateTime),
                    new SqlParameter("@huizhiurl", SqlDbType.NVarChar,950),
                    new SqlParameter("@number", SqlDbType.NVarChar,10)};
            parameters[0].Value = model.instance_id;
            parameters[1].Value = model.case_id;
            parameters[2].Value = model.notice_code;
            parameters[3].Value = model.notified_name;
            parameters[4].Value = model.company_name;
            parameters[5].Value = model.causes;
            parameters[6].Value = model.zhifarenyuan;
            parameters[7].Value = model.add_time;
            parameters[8].Value = model.huizhiurl;
            parameters[9].Value = model.number;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Models.case_info_notice model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update eazy_case_info_notice set ");
            strSql.Append("notice_code=@notice_code,");
            strSql.Append("notified_name=@notified_name,");
            strSql.Append("company_name=@company_name,");
            strSql.Append("causes=@causes,");
            strSql.Append("zhifarenyuan=@zhifarenyuan,");
            strSql.Append("add_time=@add_time,");
            strSql.Append("huizhiurl=@huizhiurl,");
            strSql.Append("number=@number");
            strSql.Append(" where instance_id=@instance_id and case_id=@case_id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@notice_code", SqlDbType.VarChar,50),
                    new SqlParameter("@notified_name", SqlDbType.NVarChar,150),
                    new SqlParameter("@company_name", SqlDbType.NVarChar,250),
                    new SqlParameter("@causes", SqlDbType.NVarChar,550),
                    new SqlParameter("@zhifarenyuan", SqlDbType.NVarChar,750),
                    new SqlParameter("@add_time", SqlDbType.DateTime),
                    new SqlParameter("@huizhiurl", SqlDbType.NVarChar,950),
                    new SqlParameter("@number", SqlDbType.NVarChar,10),
                    new SqlParameter("@id", SqlDbType.Int,4),
                    new SqlParameter("@instance_id", SqlDbType.Int,4),
                    new SqlParameter("@case_id", SqlDbType.Int,4)};
            parameters[0].Value = model.notice_code;
            parameters[1].Value = model.notified_name;
            parameters[2].Value = model.company_name;
            parameters[3].Value = model.causes;
            parameters[4].Value = model.zhifarenyuan;
            parameters[5].Value = model.add_time;
            parameters[6].Value = model.huizhiurl;
            parameters[7].Value = model.number;
            parameters[8].Value = model.id;
            parameters[9].Value = model.instance_id;
            parameters[10].Value = model.case_id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int instance_id, int case_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from eazy_case_info_notice ");
            strSql.Append(" where instance_id=@instance_id and case_id=@case_id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4),
                    new SqlParameter("@case_id", SqlDbType.Int,4)           };
            parameters[0].Value = instance_id;
            parameters[1].Value = case_id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.case_info_notice GetModel(int instance_id, int case_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,instance_id,case_id,notice_code,notified_name,company_name,causes,zhifarenyuan,add_time,huizhiurl,number from eazy_case_info_notice ");
            strSql.Append(" where instance_id=@instance_id and case_id=@case_id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4),
                    new SqlParameter("@case_id", SqlDbType.Int,4)           };
            parameters[0].Value = instance_id;
            parameters[1].Value = case_id;

            Models.case_info_notice model = new Models.case_info_notice();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Models.case_info_notice GetModel_I(int instance_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,instance_id,case_id,notice_code,notified_name,company_name,causes,zhifarenyuan,add_time,huizhiurl,number from eazy_case_info_notice ");
            strSql.Append(" where instance_id=@instance_id ");
            SqlParameter[] parameters = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4)};
            parameters[0].Value = instance_id;

            Models.case_info_notice model = new Models.case_info_notice();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.case_info_notice DataRowToModel(DataRow row)
        {
            Models.case_info_notice model = new Models.case_info_notice();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["instance_id"] != null && row["instance_id"].ToString() != "")
                {
                    model.instance_id = int.Parse(row["instance_id"].ToString());
                }
                if (row["case_id"] != null && row["case_id"].ToString() != "")
                {
                    model.case_id = int.Parse(row["case_id"].ToString());
                }
                if (row["notice_code"] != null)
                {
                    model.notice_code = row["notice_code"].ToString();
                }
                if (row["notified_name"] != null)
                {
                    model.notified_name = row["notified_name"].ToString();
                }
                if (row["company_name"] != null)
                {
                    model.company_name = row["company_name"].ToString();
                }
                if (row["causes"] != null)
                {
                    model.causes = row["causes"].ToString();
                }
                if (row["zhifarenyuan"] != null)
                {
                    model.zhifarenyuan = row["zhifarenyuan"].ToString();
                }
                if (row["add_time"] != null && row["add_time"].ToString() != "")
                {
                    model.add_time = DateTime.Parse(row["add_time"].ToString());
                }
                if (row["huizhiurl"] != null)
                {
                    model.huizhiurl = row["huizhiurl"].ToString();
                }
                if (row["number"] != null)
                {
                    model.number = row["number"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,instance_id,case_id,notice_code,notified_name,company_name,causes,zhifarenyuan,add_time,huizhiurl,number ");
            strSql.Append(" FROM eazy_case_info_notice ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,instance_id,case_id,notice_code,notified_name,company_name,causes,zhifarenyuan,add_time,huizhiurl,number ");
            strSql.Append(" FROM eazy_case_info_notice ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM eazy_case_info_notice ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.case_id desc");
            }
            strSql.Append(")AS Row, T.*  from eazy_case_info_notice T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion  BasicMethod
    }
}
